package com.tnsw.noxnox.facebook;

import java.util.Arrays;

import org.json.JSONArray;
import org.json.JSONObject;

import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.internal.SessionTracker;
import com.facebook.model.GraphObject;
import com.facebook.model.GraphUser;
import com.tnsw.noxnox.R;
import com.tnsw.noxnox.R.layout;
import com.tnsw.noxnox.R.menu;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class FacebookLoginActivity extends Activity {

	private String tag = "FacebookLoginActivity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(tag, "onCreate()");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_facebook_login);
		
		Session.OpenRequest openRequest = null;
		openRequest = new Session.OpenRequest(this);
		openRequest.setPermissions(Arrays.asList("email", "read_stream", "publish_stream"));

		Session session;
		SessionTracker tracker = new SessionTracker(this, new Session.StatusCallback() {
			
			@Override
			public void call(Session session, SessionState state, Exception exception) {
				
			}
		});
		
		session = tracker.getOpenSession();
		
		if (session == null) {
			Log.d(tag, "Creating new FaceBook Session");
			session = new Session(this);
			session.addCallback(new Session.StatusCallback() {
				
				//Session.openActiveSession(this, true, new Session.StatusCallback() {
					
					@Override
					public void call(Session session, SessionState state, Exception exception) {
						Log.d(tag, "StatusCallback().call() is being called.");
						Session.setActiveSession(session);
					    Log.d(tag, "session.isOpened=" + session.isOpened() + ", session.isClosed=" + session.isClosed() + ", session.token=" + session.getAccessToken());
						
						if (session.isOpened()) {
							Log.d(tag, "Opened");
							
							
							Log.d(tag, "Calling executeMeRequestAsync()..");
							
							Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
																
								@Override
								public void onCompleted(GraphUser user, Response response) {
									if (user != null) {								
										Log.d(tag, "onCompleted, user=" + user.getName() + "inner=" + user.getInnerJSONObject().toString());	
									} else {
										Log.d(tag, "onCompleted, user == null");
										
										
									}
									
									
								}
							});
						
							String path = "me/home";
							//path = "100000209276886/feed";
							path = "10200427576830302/likes";
							
							

							
							
							Request.executeGraphPathRequestAsync(session, path, new Request.Callback() {
								
								@Override
								public void onCompleted(Response response) {
									Log.d(tag, "FQL:onCompleted:" + response.toString());
									if (response.getGraphObject() != null) {
										JSONArray ja = (JSONArray) response.getGraphObject().getProperty("data");
										Log.d(tag, "length=" + ja.length());
										int i;
										for (i = 0; i < ja.length(); i++) {
											try {
												JSONObject jo = (JSONObject) ja.get(i);
												Log.d(tag, jo.toString());
											} catch (Exception e) {
												
											}
											
										}
										
									} else {
										Log.d(tag, "getGraphObject() is null");
									}
																											
								}
							});
							
						} else if (session.isClosed()) {
							Log.d(tag, "Closed");
						}
						
						
						
					}
				});
				
				//session.openForRead(openRequest);
				session.openForPublish(openRequest);
				Log.d(tag, "herexx");			
		} else {
			
			//session = Session.getActiveSession();
			Log.d(tag, "Retrived existing Active Session");
			Log.d(tag, "existing, session.isOpened=" + session.isOpened() + ", session.isClosed=" + session.isClosed() + ", session.token=" + session.getAccessToken());
			Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
				
				@Override
				public void onCompleted(GraphUser user, Response response) {
					if (user != null) {								
						Log.d(tag, "(existing) onCompleted, user=" + user.getName() + "inner=" + user.getInnerJSONObject().toString());	
					} else {
						Log.d(tag, "(existing) onCompleted, user == null");
					}
					
					
				}
			});
			
			Request request = new Request(session, "10200427576830302/likes", null, HttpMethod.POST, new Request.Callback() {
				
				@Override
				public void onCompleted(Response response) {
					Log.d(tag, "um..=" + response.getRequest().getSession().getAccessToken());
					
				}
			});
			
			RequestAsyncTask task = new RequestAsyncTask(request);
			task.execute();
		}
		

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		Log.d(tag, "requestCode=" + requestCode + ", resultCode=" + resultCode);
		
		Session session = Session.getActiveSession();
		if (session != null) {
			
			session.onActivityResult(this, requestCode, resultCode, data);
			Log.d(tag, "onActivityResult OK");
		} else {
			Log.d(tag, "ActiveSession is null");
		}
		
		
	}	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.facebook_login, menu);
		return true;
	}

}
